#!/usr/bin/env python
#-*-encoding:utf-8-*-
'''
Created on 2015年8月12日

@author: chenyongbing
'''
import sys,os,re
#current_dir = os.path.dirname(__file__)
current_dir = os.path.split(os.path.realpath(__file__))[0]

import logging
import hashlib
import gzip

sys.path.append(os.path.join(current_dir,'../'))
from base.strChar import *

class encryptUploadFile():
    
    def open_file(self,ifile=''):
        if re.search('.gz$',ifile):
            with gzip.open(ifile, 'rb') as f:
                for line in f.readlines():
                    yield line
        else:
            for line in open(ifile).readlines():
                yield line
    
    def encrypt_mobile(self,import_file='',export_file='',max_line_cnt=10000):
        fr = open(export_file,'w')
        count = 0
        #for line in open(import_file).readlines():
        for line in self.open_file(import_file):
            mobile = line.strip()
            if not re.search('^[\d]{11}$',mobile):
                logging.warn('Phone num  %s is Invalid.'%mobile)
                continue
    #         print mobile
    #         print hashlib.sha1(mobile).hexdigest()
    #         print hashlib.md5( hashlib.sha1(mobile).hexdigest() ).hexdigest()
            mobile_str =hashlib.md5( mobile ).hexdigest()
            count+=1
            fr.write( '1#1002#%s,%s\n'%(mobile_str,count))
            if max_line_cnt!=None and count>=max_line_cnt:break
        fr.close()
        return count
 
 
    def check_mac_global_or_local(self,mac):
        b = hex2bin(mac[:2])
        print mac[:2], b
        if int(b[6])==0:
            return 'global'
        else:
            return 'local'
        
        
    def encrypt_mac(self,import_file='',export_file='',max_line_cnt=10000):
        fr = open(export_file,'w')
        count = 0
        #for line in open(import_file).readlines():
        for line in self.open_file(import_file):
            line = line.strip()
            mac = re.sub(':','',line)
            if len(mac)!=12:
                logging.warn('Mac %s is Invalid.'%line)
                continue
            new_mac = ':'.join((re.findall('.{2}',mac.upper())))
            if self.check_mac_global_or_local(new_mac)=='local':continue
            
            mac_str = hashlib.sha1(new_mac).hexdigest()
            count+=1
            fr.write( '%s,%s\n'%(mac_str,count))
            if max_line_cnt!=None and count>=max_line_cnt:break
        fr.close()
        return count
    
    def encrypt_str_with_upper_or_lower(self,import_file='',export_file='',max_line_cnt=10000,upper_or_lower='upper'):
        fr = open(export_file,'w')
        count = 0
        #for line in open(import_file).readlines():
        for line in self.open_file(import_file):
            if upper_or_lower == 'upper':
                line = line.strip().upper()
            elif upper_or_lower == 'lower':
                line = line.strip().lower()
            else:
                line = line.strip()
            count+=1
            fr.write( '%s,%s\n'%(line,count))
            if max_line_cnt!=None and count>=max_line_cnt:break
        fr.close()
        return count
    
    def is_valid_file(self,parser, arg):
        if not os.path.exists(arg):
            parser.error("The file %s does not exist!" % arg)      
        else:
            return arg
            
            
    def is_valid_scale(self,parser,arg):
        scaleList  = arg.split(':')
        if len(scaleList)!=2:
            parser.error("scale %s is invalid!" % arg)
        min_line_cnt,max_line_cnt =  scaleList
        if re.search('^[\d]+$',min_line_cnt) and re.search('^[\d]+$',max_line_cnt):
            return int(min_line_cnt),int(max_line_cnt)
        else:
            parser.error("scale %s is invalid!" % arg)
            
            
    def encrypt_and_update_tab(self,import_file='',export_file='',data_type='mac',max_line_cnt=10000,min_line_cnt=0,demand_id='testing'):
        '''mac/mobile/imei/email/idfa/imsi'''
        
        
        if os.path.exists(export_file):
            os.remove(export_file)
        if data_type == 'mac':
            line_cnt = self.encrypt_mac(import_file=import_file,export_file=export_file,max_line_cnt=max_line_cnt)
        elif data_type == 'mobile':
            line_cnt = self.encrypt_mobile(import_file=import_file,export_file=export_file,max_line_cnt=max_line_cnt)
        
        elif data_type == 'imei':
            line_cnt = self.encrypt_str_with_upper_or_lower(import_file=import_file,export_file=export_file,max_line_cnt=max_line_cnt)
        elif data_type == 'email':
            line_cnt = self.encrypt_str_with_upper_or_lower(import_file=import_file,export_file=export_file,max_line_cnt=max_line_cnt,upper_or_lower='lower')
        elif data_type == 'idfa':
            line_cnt = self.encrypt_str_with_upper_or_lower(import_file=import_file,export_file=export_file,max_line_cnt=max_line_cnt,upper_or_lower='upper')
        elif data_type == 'imsi':
            line_cnt = self.encrypt_str_with_upper_or_lower(import_file=import_file,export_file=export_file,max_line_cnt=max_line_cnt)
        else:
            logging.error('data_type %s Invalid')
            print 'data_type %s Invalid'
            #sys.exit(1)
            return False
        if  min_line_cnt!=None and line_cnt < min_line_cnt:
            logging.error('line_cnt %s < min_line_cnt %s'%(line_cnt,min_line_cnt))
            #sys.exit(1)
            
#         if os.path.exists(export_file):
#             
#             data_size = os.path.getsize(export_file)
#             
#             query = "update analysis set data_size='%s',line_cnt='%s',file_path='%s',status=1,encrypt=2 where data_name='%s'"%(data_size,line_cnt,export_file,demand_id)
#             logging.info(query)
        return True
    
    def main(self,args):
        import_file = args.import_file
        data_type = args.data_type
        demand_id = args.demand_id
        export_path = args.export_path
        comp_id = args.comp_id
        min_line_cnt , max_line_cnt = args.scale
        today = datetime.datetime.today().strftime('%Y%m%d%H%M')
        export_file = export_path +'%s.%s.%s.%s.txt'%(data_type,str(comp_id),str(demand_id),str(today))
    
            
myEncryptUploadFile = encryptUploadFile()



if __name__== '__main__':
    sys.path.append(os.path.join(current_dir,'../'))
    from base.mylogging import *
    import argparse,re,datetime,time
    
    parser = argparse.ArgumentParser(description='encrypt customer upload file')
    parser.add_argument('--import_file',metavar="导入文件",required=True,type=lambda x: myEncryptUploadFile.is_valid_file(parser, x))
    parser.add_argument('--data_type', action='store', dest='data_type', help='数据类型', choices=['mobile', 'mac','imei','email','idfa','imsi'], required=True)
    parser.add_argument('--demand_id',metavar='需求ID',required=True)
    parser.add_argument('--comp_id',metavar='公司ID',required=True)
    parser.add_argument('--export_path',metavar='导出目录',default='/data/tmp/')
    parser.add_argument('--scale',metavar='分析规模',default='0:10000',type=lambda x:myEncryptUploadFile.is_valid_scale(parser,x))
    args = parser.parse_args()
    logging = logging_stdout_and_logfile(level = 'info' , logfile = '/data/logs/encrypt_customer_upload_file.log')
    myEncryptUploadFile.main(args)
    
    